package framework.system.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import framework.core.controller.BaseAction;
import framework.core.util.Kit;
import framework.core.util.ListRange;
import framework.system.entity.SysResource;
import framework.system.entity.SysUser;
import framework.system.entity.front.TreeNode;
import framework.system.service.IMenuService;

/** 
 * @ClassName: MenuController 
 * @Description: 资源Controller
 * @author: Ching Wang
 * @date 2013-5-3 下午4:01:05 
 *  
 */
@Controller
@RequestMapping(value="/menuctrl")
public class MenuController extends BaseAction{
	@Resource private IMenuService menuService;
	
	/**
	 * @Title: showPage 
	 * @Description: 获取用户功能，并返回页面 
	 * @param page 页面路径
	 * @param resId 资源ID
	 * @param session
	 * @param request
	 * @param response
	 * @return ModelAndView
	 * @throws Exception
	 */
	@RequestMapping(value = "/showPage", method = RequestMethod.GET)
	public ModelAndView showPage(String page,String resId, HttpSession session, HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView mv = new ModelAndView(page);
		List<SysResource> opers = new ArrayList<SysResource>();
		SysUser user = (SysUser) session.getAttribute("user");
		if(user != null){
			opers = menuService.loadOper(user.getId(),resId);
		}
		mv.addObject("opers", opers);
		
		return mv;
	}
	
	/**
	 * @Title: loadMenu 
	 * @Description: 加载菜单 
	 * @param session
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/loadmenu")
	public void loadMenu(HttpSession session, HttpServletRequest request, HttpServletResponse response) throws Exception{
		List<TreeNode> treeNodes = new ArrayList<TreeNode>();
		
		SysUser user = (SysUser) session.getAttribute("user");
		if(user != null){
			treeNodes = menuService.loadTree(user.getId(),"menu");
		}
		outJsonArray(response, treeNodes, "yyyy-MM-dd");
	}
	
	/**
	 * @Title: listTree 
	 * @Description: 查询资源树
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/listTree")
	public void listTree(HttpServletRequest request, HttpServletResponse response) throws Exception{
		List<TreeNode> treeNodes = menuService.loadTree("","all");
		outJsonArray(response, treeNodes, "yyyy-MM-dd");
	}
	
	/**
	 * @Title: editRes 
	 * @Description: 编辑资源信息
	 * @param node 树节点属性集合TreeNode
	 * @param oper 操作类型（add,up）
	 * @param session
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/editRes")
	public void editRes(TreeNode node, String oper, HttpSession session, HttpServletRequest request, HttpServletResponse response) throws Exception{
		SysResource resource = new SysResource();
		SysResource opres = new SysResource();
		if(Kit.isNotNull(node.getId())){
			resource = menuService.getResource(node.getId());
			opres = resource.getSysResource();
		}
		
		if(Kit.isNotNull(node.getPid())){
			SysResource pres = menuService.getResource(node.getPid());
			if(pres != null){
				resource.setSysResource(pres);
			}
		}
		resource.setIconCls(node.getIconCls());
		resource.setMethod(node.getMethod());
		resource.setRemark(node.getRemark());
		
		resource.setText(node.getText());
		resource.setType(node.getType());
		resource.setUrl(node.getUrl());
		
		menuService.saveOrUpdate(resource,opres);
		JSONObject obj = new JSONObject();
		obj.put("success", true);
		if(Kit.isNotNull(oper) && "add".equals(oper)){
			obj.put("message", "添加资源信息成功。");
		}
		if(Kit.isNotNull(oper) && "edit".equals(oper)){
			obj.put("message", "更新资源信息成功。");
		}
		outJson(response, obj, "yyyy-MM-dd");
	}
	/**
	 * @Title: delRes 
	 * @Description: 删除资源信息
	 * @param resIds 资源编号集合
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/delRes")
	public void delRes(String resIds, HttpServletRequest request, HttpServletResponse response) throws Exception{
		ListRange<TreeNode> range = new ListRange<TreeNode>();
		if(Kit.isNotNull(resIds)){
			range = menuService.delRes(resIds.split(","));
			if(range.isSuccess()){
				range.setMessage("删除资源信息成功.");
			}
		}
		else{
			range.setMessage("未找到相关资源，请确认.");
		}
		outJson(response, range, "yyyy-MM-dd");
	}
	
	/**
	 * @Title: seqRes 
	 * @Description: 调整资源树节点顺序
	 * @param resId 资源节点编号
	 * @param type 调整方式（up：上移，down：下移）
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/seqRes")
	public void seqRes(String resId, String type, HttpServletRequest request, HttpServletResponse response) throws Exception{
		ListRange<SysResource> range = new ListRange<SysResource>();
		if(Kit.isNotNull(resId)){
			range = menuService.seqRes(resId,type);
			if(range.isSuccess()){
				range.setMessage("移动资源信息成功.");
			}
		}
		else{
			range.setMessage("未找到相关资源，请确认.");
		}
		outJson(response, range, "yyyy-MM-dd");
	}
}


